﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iAnywhere.Data.SQLAnywhere;
using Oracle.DataAccess.Client;
using System.Configuration;
using Syncer;
using System.Globalization;
using System.IO;
using System.Data;



namespace SybaseTest
{
    class Nd_Performance_Collector
    {


        static void Main(string[] args)
        {
            Log.OpenLog();
            string bppmConn = string.Empty;
            string oracleConn = string.Empty;
            string sqlQuery = string.Empty;
           

            foreach (var Key in ConfigurationManager.AppSettings.Keys)
            {
                if (Key.ToString() == "福建省烟草专卖局")
                {
                    bppmConn = ConfigurationManager.AppSettings[Key.ToString()];
                    sqlQuery = @"select * from eg_event_view where mc_owner='福建省烟草专卖局' and SEVERITY!='OK' and SEVERITY!='UNKNOWN' and SEVERITY!='INFO' ";
                }
                else if (Key.ToString() == "unify")
                {
                    oracleConn = ConfigurationManager.AppSettings["unify"];
                }
                else
                {
                    bppmConn = ConfigurationManager.AppSettings[Key.ToString()];
                    sqlQuery = @"select * from eg_event_view where SEVERITY!='OK' and SEVERITY!='UNKNOWN' and SEVERITY!='INFO' ";
                }

                List<ndPerformanceEntity> ndpe = new List<ndPerformanceEntity>();

                try
                {
                    ndpe = LoadEgEvent(bppmConn, sqlQuery);

                }
                catch (Exception ex)
                {
                    Log.Exception("", ex);
                    Log.CloseLog();
                }

                try
                {
                    LoadAlertTable(oracleConn, ndpe);

                }
                catch (Exception ex)
                {
                    Log.Exception("", ex);
                    Log.CloseLog();
                }
            }
        }
        static List<ndPerformanceEntity> LoadEgEvent(string bppmStr, string sql)
        {

            List<ndPerformanceEntity> ndpe = new List<ndPerformanceEntity>();
            using (SAConnection conn = new SAConnection(bppmStr))
            {
                Log.Info("connect " + bppmStr);
                conn.Open();

               // Log.Info("Now is get egevent");

                SACommand cmd = new SACommand(sql, conn);
                SADataReader rd = cmd.ExecuteReader();


                while (rd.Read())
                {
                    ndpe.Add(new ndPerformanceEntity
                    {


                        EVENT_HANDLE = rd["EVENT_HANDLE"].ToString(),
                        EVENTCLASS = rd["EVENTCLASS"].ToString(),
                        MC_CLIENT_ADDRESS = rd["MC_CLIENT_ADDRESS"].ToString(),
                        MC_HOST = rd["MC_HOST"].ToString(),
                        MC_HOST_ADDRESS = rd["MC_HOST_ADDRESS"].ToString(),
                        MC_OBJECT_CLASS = rd["MC_OBJECT_CLASS"].ToString(),
                        MC_OBJECT_OWNER = rd["MC_OBJECT_OWNER"].ToString(),
                        MC_OBJECT = rd["MC_OBJECT"].ToString(),
                        MC_TOOL_CLASS = rd["MC_TOOL_CLASS"].ToString(),
                        MC_TOOL_SEV = rd["MC_TOOL_SEV"].ToString(),
                        MC_ORIGIN_CLASS = rd["MC_ORIGIN_CLASS"].ToString(),
                        MC_PARAMETER = rd["MC_PARAMETER"].ToString(),
                        MC_PARAMETER_VALUE = rd["MC_PARAMETER_VALUE"].ToString(),
                        MC_INCIDENT_TIME = rd["MC_INCIDENT_TIME"].ToString(),
                        MC_ARRIVAL_TIME = rd["MC_ARRIVAL_TIME"].ToString(),
                        MC_LOCAL_RECEPTION_TIME = rd["MC_LOCAL_RECEPTION_TIME"].ToString(),
                        MC_DATE = rd["MC_DATE"].ToString(),
                        DATE_RECEPTION = rd["DATE_RECEPTION"].ToString(),
                        STATUS = rd["STATUS"].ToString(),
                        SEVERITY = rd["SEVERITY"].ToString(),
                        MC_OWNER = rd["MC_OWNER"].ToString(),
                        MSG = rd["MSG"].ToString(),
                        MC_LONG_MSG = rd["MC_LONG_MSG"].ToString(),
                        MC_DATE_MODIFICATION = rd["MC_DATE_MODIFICATION"].ToString()

                    });

                }
                conn.Close();
              //  Log.Info("Now is load list");
            }

            return ndpe;
        }




        static void LoadAlertTable(string unifyshowStr, List<ndPerformanceEntity> ndpe)
        {
            int insertcount = 0;
            int updatecount = 0;
            int scapecount = 0;
            using (OracleDataAccess oc = new OracleDataAccess())
            {
                oc.Open();

                //将list中的数据插入到eg_event中

                foreach (var eg_event in ndpe)
                {
                    try
                    {
                        String EVENT_HANDLE = eg_event.EVENT_HANDLE.Replace("'", "''");
                        String EVENTCLASS = eg_event.EVENTCLASS.Replace("'", "''");
                        String MC_CLIENT_ADDRESS = eg_event.MC_CLIENT_ADDRESS.Replace("'", "''");
                        String MC_HOST = eg_event.MC_HOST.Replace("'", "''");
                        String MC_HOST_ADDRESS = eg_event.MC_HOST_ADDRESS.Replace("'", "''");
                        String MC_OBJECT_CLASS = eg_event.MC_OBJECT_CLASS.Replace("'", "''");
                        String MC_OBJECT_OWNER = eg_event.MC_OBJECT_OWNER.Replace("'", "''");
                        String MC_OBJECT = eg_event.MC_OBJECT.Replace("'", "''");
                        String MC_TOOL_CLASS = eg_event.MC_TOOL_CLASS.Replace("'", "''");
                        String MC_TOOL_SEV = eg_event.MC_TOOL_SEV.Replace("'", "''");
                        String MC_ORIGIN_CLASS = eg_event.MC_ORIGIN_CLASS.Replace("'", "''");
                        String MC_PARAMETER = eg_event.MC_PARAMETER.Replace("'", "''");
                        String MC_PARAMETER_VALUE = eg_event.MC_PARAMETER_VALUE.Replace("'", "''");
                        String MC_INCIDENT_TIME = eg_event.MC_INCIDENT_TIME.Replace("'", "''");
                        String MC_ARRIVAL_TIME = eg_event.MC_ARRIVAL_TIME.Replace("'", "''");
                        String MC_LOCAL_RECEPTION_TIME = eg_event.MC_LOCAL_RECEPTION_TIME.Replace("'", "''");
                        String DATE_RECEPTION = eg_event.DATE_RECEPTION.Replace("'", "''");
                        String MC_DATE = eg_event.MC_DATE.Replace("'", "''");
                        String STATUS = eg_event.STATUS.Replace("'", "''");
                        String SEVERITY = eg_event.SEVERITY.Replace("'", "''");
                        String MC_OWNER = eg_event.MC_OWNER.Replace("'", "''");
                        String MSG = eg_event.MSG.Replace("'", "''");
                        String MC_LONG_MSG = eg_event.MC_LONG_MSG.Replace("'", "''");
                        String MC_DATE_MODIFICATION = eg_event.MC_DATE_MODIFICATION.Replace("'", "''");

                        string sqlcheck = @"select * from eg_event where EVENT_HANDLE='" + EVENT_HANDLE + "'";
                        DataTable egevents = oc.ExecuteFillToDataTable(sqlcheck);
                        if (egevents.Rows.Count == 0)
                        {
                            string sql5 = string.Empty;
                            sql5 = @"insert into eg_event (EVENT_HANDLE,EVENTCLASS,MC_CLIENT_ADDRESS,MC_HOST,MC_HOST_ADDRESS,MC_OBJECT_CLASS,MC_OBJECT_OWNER,MC_OBJECT,MC_TOOL_CLASS,MC_TOOL_SEV,MC_ORIGIN_CLASS,MC_PARAMETER,MC_PARAMETER_VALUE,MC_INCIDENT_TIME,MC_ARRIVAL_TIME,MC_LOCAL_RECEPTION_TIME,DATE_RECEPTION,MC_DATE,STATUS,SEVERITY,MC_OWNER,MSG,MC_LONG_MSG,MC_DATE_MODIFICATION) values ('" + EVENT_HANDLE + "','" + EVENTCLASS + "','" + MC_CLIENT_ADDRESS + "','" + MC_HOST + "','" + MC_HOST_ADDRESS + "','" + MC_OBJECT_CLASS + "','" + MC_OBJECT_OWNER + "','" + MC_OBJECT + "','" + MC_TOOL_CLASS + "','" + MC_TOOL_SEV + "','" + MC_ORIGIN_CLASS + "','" + MC_PARAMETER + "','" + MC_PARAMETER_VALUE + "','" + MC_INCIDENT_TIME + "','" + MC_ARRIVAL_TIME + "','" + MC_LOCAL_RECEPTION_TIME + "','" + DATE_RECEPTION + "','" + MC_DATE + "','" + STATUS + "','" + SEVERITY + "','" + MC_OWNER + "','" + MSG + "','" + MC_LONG_MSG + "','" + MC_DATE_MODIFICATION + "')";
                            oc.ExecuteReader(sql5);
                        //  Log.Info("insert into eg_event:" + EVENT_HANDLE);
                            insertcount++;

                        }
                        else
                        {
                            DataRow dr = egevents.Rows[0];
                            if (!string.Equals(dr["STATUS"].ToString(), STATUS))
                            {
                                string sqlupdate = string.Empty;
                                sqlupdate = @"update eg_event set STATUS='" + STATUS + "',MC_DATE_MODIFICATION='" + MC_DATE_MODIFICATION + "' where EVENT_HANDLE='" + EVENT_HANDLE + "'";
                                oc.ExecuteReader(sqlupdate);
                               // Log.Info("update eg_event:"+EVENT_HANDLE+" STATUS:" + STATUS);
                              updatecount++;
                            }
                            else
                            {
                               // Log.Info("scape eg_event:" + EVENT_HANDLE);
                                scapecount++;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception("", ex);
                        Log.CloseLog();
                    }
                }
                Log.Info("insertcount is:" + insertcount);
                Log.Info("updatecout is :" + updatecount);
                Log.Info("scapecount is :" + scapecount);
                oc.Close();
            }

        }

    }
}


